package com.wlw.layoutmanager.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wlw.layoutmanager.R;
import com.wlw.layoutmanager.adapters.PickerAdapter;
import com.wlw.layoutmanager.layoutManager.PickerLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class PickerFragment extends Fragment {
    private RecyclerView mRecyclerView1, mRecyclerView2;
    private TextView mTvHour, mTvMinute;
    private PickerLayoutManager pickerLayoutManager1, pickerLayoutManager2;
    private List<String> mHours, mMinutes;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_picker, container, false);
        initView(view);
        initListener();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    private void initView(View view) {
        mRecyclerView1 = view.findViewById(R.id.recyclerView1);
        mRecyclerView2 = view.findViewById(R.id.recyclerView2);
        mTvHour = view.findViewById(R.id.tv_hour);
        mTvMinute = view.findViewById(R.id.tv_minute);
        pickerLayoutManager1 = new PickerLayoutManager(getContext(), mRecyclerView1, PickerLayoutManager.VERTICAL, false, 3, 0.4f, true);
        mRecyclerView1.setLayoutManager(pickerLayoutManager1);
        mRecyclerView1.setAdapter(new PickerAdapter(getHours()));

        pickerLayoutManager2 = new PickerLayoutManager(getContext(), mRecyclerView2, PickerLayoutManager.VERTICAL, false, 2, 0.4f, false);
        mRecyclerView2.setLayoutManager(pickerLayoutManager2);
        mRecyclerView2.setAdapter(new PickerAdapter(getMinutes()));
    }

    private List<String> getHours() {
        mHours = new ArrayList<>();
        for (int i = 0; i <= 24; i++) {
            if (i <= 9) {
                mHours.add("0" + i);
            } else {
                mHours.add(String.valueOf(i));
            }
        }
        return mHours;
    }

    private List<String> getMinutes() {
        mMinutes = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if (i <= 9)
                mMinutes.add("0" + i);
            else
                mMinutes.add(String.valueOf(i));
        }
        return mMinutes;
    }

    private void initListener() {
        pickerLayoutManager1.setOnSelectedViewListener((view, position) -> {

            mTvHour.setText(mHours.get(position));
        });
        pickerLayoutManager2.setOnSelectedViewListener((view, position) -> {
            mTvMinute.setText(mMinutes.get(position));
        });
    }
}
